package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UserParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object detail(String id) {

        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    //因为有分页所以用StrategyQuery来封装
    public Object query(StrategyQuery qo) {
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes() {
        return JsonResult.success(strategyThemeService.list());
    }

    //攻略评论添加
    @PostMapping("/addComment")
    @RequiredLogin
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        //把userInfo的内容复制到strategyComment对象
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());
        //设置评论创建时间
        strategyComment.setCreateTime(new Date());
        strategyCommentService.save(strategyComment);
        //评论完成后品论数+1
        strategyStatisVORedisService.increaseReplynum(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }

    @GetMapping("/comments")
    //攻略评论分页展示
    public Object comments(StrategyCommentQuery qo) {
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @PostMapping("/commentThumb")
    //点赞和取消点赞处理
    @RequiredLogin
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo) {
        strategyCommentService.commentThumb(cid, sid, userInfo.getId());

        return JsonResult.success();
    }
//阅读数添加
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        strategyStatisVORedisService.increaseViewnum(sid, 1);
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVO(sid));
    }
    //收藏和取消收藏
    @RequiredLogin
    @PostMapping ("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo) {
      //返回值布尔类型，true表示收藏成功
        return JsonResult.success(strategyStatisVORedisService.favor(sid,userInfo.getId()));
    }
    //点赞操作
    @RequiredLogin
    @PostMapping ("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo) {
        //返回值布尔类型，true表示点赞成功
        return JsonResult.success(strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId()));
    }
}
